package com.sneakxy.cloudbase.platform.config;

import java.nio.charset.Charset;
import java.util.List;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.web.servlet.MultipartProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Lazy;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.StringHttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.web.servlet.config.annotation.DefaultServletHandlerConfigurer;
import org.springframework.web.servlet.config.annotation.DelegatingWebMvcConfiguration;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping;
import org.springframework.web.servlet.view.InternalResourceViewResolver;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.sneakxy.cloudbase.platform.handler.CloudBaseRequestMappingHandlerMapping;
import com.sneakxy.cloudbase.platform.handler.StandardServletMultipartResolverHandler;

@Configuration
public class ServletContextConfig extends DelegatingWebMvcConfiguration {
	
	public static final String DEFAULT_SERVLET_NAME = "Cloud Base Producder Servelt";

	@Value("${spring.http.encoding.charset}")
	private String encoding;
	
	@Override
	protected RequestMappingHandlerMapping createRequestMappingHandlerMapping() {
		return new CloudBaseRequestMappingHandlerMapping();
	}

	@Override
	protected void addResourceHandlers(ResourceHandlerRegistry registry) {
		super.addResourceHandlers(registry);
		registry.addResourceHandler("/**")
			.addResourceLocations("classpath:/ui/");
		registry.addResourceHandler("doc.html")
			.addResourceLocations("classpath:/META-INF/resources/");
		registry.addResourceHandler("/webjars/**")
			.addResourceLocations("classpath:/META-INF/resources/webjars/");
	}

	@Override
	protected void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
		configurer.enable(DEFAULT_SERVLET_NAME);
		super.configureDefaultServletHandling(configurer);
	}
	
	@Override
    public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
        super.configureMessageConverters(converters);
        converters.add(responseBodyConverter());
        converters.add(mappingJackson2HttpMessageConverter());
    }
    
    @Bean
    public MappingJackson2HttpMessageConverter mappingJackson2HttpMessageConverter() {
    	return new MappingJackson2HttpMessageConverter(new ObjectMapper());
    }
    
    @Bean
    public HttpMessageConverter<String> responseBodyConverter() {
        return new StringHttpMessageConverter(Charset.forName(this.encoding));
    }
    
    @Bean
    public InternalResourceViewResolver internalResourceViewResolver() {
    	InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
    	viewResolver.setPrefix("/WEB-INF/views/");
    	viewResolver.setSuffix(".jsp");
    	return viewResolver;
    }
    
    @Lazy
    @Bean
    public StandardServletMultipartResolverHandler multipartResolver(MultipartProperties multipartProperties) {
    	StandardServletMultipartResolverHandler multipartResolver = new StandardServletMultipartResolverHandler();
		multipartResolver.setResolveLazily(multipartProperties.isResolveLazily());
		return multipartResolver;
    }
    
}
